package com.bibrApp.difu.actvity.beginner_guide_mvp;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.bibrApp.difu.Response.LoginResponseBody;
import com.bibrApp.difu.Response.PaySettingResPonseBody;
import com.bibrApp.difu.api.RetrofitNet;
import com.bibrApp.difu.base.BaseObserver;
import com.bibrApp.difu.base.BasePresenter;
import com.bibrApp.difu.utils.EncryptUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class BeginnerGuidePresenter extends BasePresenter<IBeginnerGuideView> {
    public BeginnerGuidePresenter(Context context, IBeginnerGuideView iView) {
        super(context, iView);
    }

    public void getAllPaySetting() {
        RetrofitNet.getInstance()
                .getApi()
                .queryAllPaySetting()
                .compose(getFragmentLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context, false) {

                    @Override
                    protected void onSuccess(Object value) {
                        if (value != null) {
                            String jsonStr = EncryptUtils.objectToJson(value);
                            if (TextUtils.isEmpty(jsonStr)) {
                                return;
                            }
                            List<PaySettingResPonseBody> datas = new Gson().fromJson(jsonStr, new TypeToken<List<PaySettingResPonseBody>>() {}.getType());
                            if (datas != null && datas.size() > 0) {
                                for (int i = 0; i < datas.size(); i++) {
                                    if (datas.get(i).getCoinEunit().equalsIgnoreCase("usdt")) {
                                        iView.successOfGetPaySetting(true);
                                        break;
                                    }
                                }

                            }
                        }

                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                    }
                });
    }

    /**
     * 查询总资产
     */
    public void queryAllAssets() {
        RetrofitNet.getInstance()
                .getApi()
                .queryAllAssets()
                .compose(getFragmentLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        if (value != null) {
                            if (Double.parseDouble(String.valueOf(value)) > 0) {
                                iView.successOfGetAllAssets(true);
                            }
                        }

                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                    }
                });

    }

    public void getUserInfo(String userId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        RetrofitNet.getInstance().getApi()
                .getAccountInfo(RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(map)))
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new BaseObserver<Object>(context) {
                    @Override
                    protected void onSuccess(Object value) {
                        String jsonStr = EncryptUtils.objectToJson(value);
                        LoginResponseBody.CenterUserMain loginRes = new Gson().fromJson(jsonStr, LoginResponseBody.CenterUserMain.class);
                        iView.successOfGetUserInfo(loginRes);
                    }

                    @Override
                    protected void onFialure(int code, String msg) {
                        super.onFialure(code, msg);
                    }
                });

    }
}
